<extend name="layout/edit" />

<block name="HEADER">
	<script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp&key=FZZBZ-APGK6-6CJSU-EXA6S-P6RN7-MBBIX"></script>
	<script charset="utf-8" src="https://map.qq.com/api/gljs?v=1.exp&key=FZZBZ-APGK6-6CJSU-EXA6S-P6RN7-MBBIX"></script>
</block>

<block name="CONTENT">
	<div id="addressDetailed" style="height: 100%;width: 100%;"></div>
</block>

<block name="FOOTER">
   <script type="text/javascript">
   	var latitude = $.getRequest('latitude');
	var longitude = $.getRequest('longitude');
	var ranges = $.getRequest('circle');
	
    var  page = (function(ns){
    	
		ns.init = function(){
			if(ranges){
				var center = new TMap.LatLng( latitude || 22.823023, longitude || 108.322449);
				var map = new TMap.Map(document.getElementById('addressDetailed'),{
					center: center,
					zoom: 11 
				});
				//编辑
			    new TMap.MultiCircle({map,
					styles: { // 设置圆形样式
						'circle': new TMap.CircleStyle({
						  	'color': 'rgba(41,91,255,0.16)',
						  	'showBorder': true,
						  	'borderColor': 'rgba(41,91,255,1)',
						  	'borderWidth': 2,
						}),
					},
					geometries: [{
						styleId: 'circle',
						center: center, //圆形中心点坐标 
						radius: ranges * 1000,	//半径（单位：米）
					}],
				});	
			}else{
				var center = new qq.maps.LatLng( latitude || 22.823023, longitude || 108.322449);
				var map = new qq.maps.Map(document.getElementById('addressDetailed'),{
					center: center,
					zoom: 11 
				});
				//调用城市服务信息
			    var citylocationMarker = null;
			    var citylocation = new qq.maps.CityService({
			        complete : function(results){
			            //设置marker标记
			            if(citylocationMarker!=null)  citylocationMarker.setMap(null);
			           	citylocationMarker = new qq.maps.Marker({
			                map: map,
			                position: results.detail.latLng
			            });
			            qq.maps.event.addListener(map, 'click', function(event) {
				            citylocationMarker.setMap(null);      
				    	});
			    		var latLng = results.detail.latLng;
			    		ns.getLocation(latLng);
			        }
			    });
			    //调用地址解析类
			    var geocoderMarker = null;
			    var geocoder = new qq.maps.Geocoder({
			        complete : function(result){
			            map.setCenter(result.detail.location);
			            if(geocoderMarker!=null)  geocoderMarker.setMap(null);
			            if(citylocationMarker!=null)  citylocationMarker.setMap(null);
			           geocoderMarker = new qq.maps.Marker({
			                map:map,
			                position: result.detail.location
			            });
			            qq.maps.event.addListener(map, 'click', function(event) {
				            geocoderMarker.setMap(null);
				    	});
				    	ns.getLocation(result.detail.location);
			        }
			    });
			    //添加监听事件   获取鼠标单击事件
			    qq.maps.event.addListener(map, 'click', function(event) {
			    	var marker=new qq.maps.Marker({
			            position:event.latLng, 
			            map:map
			       	}); 
			       	qq.maps.event.addListener(map, 'click', function(event) {
			            marker.setMap(null);      
			    	});
			    	ns.getLocation(event.latLng);
			    });
				//编辑 - 标点
			    if(latitude){
					var marker = new qq.maps.Marker({ position:center, map:map }); 
			       	qq.maps.event.addListener(map, 'click', function(event) {
			            marker.setMap(null);      
			    	});
					ns.getLocation(center);
				}
			}
		}
		
		//获取地址
		ns.getLocation = function(latLng,index){
			var latLngs = latLng.lat +","+latLng.lng;
			parent.$(".qq_maps_latLng").remove();
		    parent.$(".layui-layer-btn").append('<div class="qq_maps_latLng" style="float: left;margin-top: 6px;">已选经纬度：<span>'+latLngs+'</span></div>');
		}
		
		ns.add = function(){
			var filter = {
	            url:"modules/lottery/edit",
	            oid:"",
	            mode:1
	        }
	        $.openIframe(filter, "新增",['750px', '580px']);
		}
		
        //保存
        ns.save=function(){
    		termForm.save(1).then(function(res){
            	termForm.opener.form.$grids['termGrid'].load();
            });
        }
        
        return ns;
    }({}));
    
    $(function(){ page.init(); });
   </script>
</block>



